<template>
    <div>
        <el-dialog v-model="props.isShow" :title="props.isAdd ? '添加活动' : '编辑活动'" :before-close="closeX"
            @open="open(formRef)">
            <el-form :model="form" label-width="80px" :rules="rules" ref="formRef">

                <el-form-item label="活动名称" prop="title">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>

                <el-form-item label="活动时间">
                    <el-date-picker v-model="time" type="datetimerange" range-separator="至" start-placeholder="开始日期"
                        end-placeholder="结束日期" @change="date">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="一级分类" prop="first_cateid">
                    <el-select v-model="form.first_cateid" placeholder="请选择一级分类" @change="change1(true)">
                        <el-option v-for="item in store.getters['cate/getCate']" :key="item.id" :label="item.catename"
                            :value="item.id" />
                    </el-select>
                </el-form-item>

                <el-form-item label="二级分类" prop="second_cateid">
                    <el-select v-model="form.second_cateid" placeholder="请选择二级分类" @change="change2(true)">
                        <el-option v-for="item in cateArr" :key="item.id" :label="item.catename" :value="item.id" />
                    </el-select>
                </el-form-item>

                <el-form-item label="商品">
                    <el-select v-model="form.goodsid" placeholder="请选择商品">
                        <el-option v-for="item in shopList" :key="item.id" :label="item.goodsname"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="状态">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
                </el-form-item>


            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="close">取消</el-button>
                    <el-button type="primary" @click="submit(formRef)">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps, FormRules, FormInstance } from 'element-plus'
import { ref, reactive, defineProps, defineEmits, defineExpose } from "vue";
import { useStore } from 'vuex';
import { specslist, goodslist, seckadd, seckedit, seckinfo } from '@/request/api';
import { et } from 'element-plus/es/locale';

let props = defineProps({
    isAdd: Boolean,
    isShow: Boolean
})
let emit = defineEmits(['close'])
let formRef = ref<FormInstance>()
let store = useStore()
let cateArr = ref()
let specsArr = ref()
let specsList = ref()
let shopList = ref()

let submit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((val) => {
        if (val) {
            if (props.isAdd) {
                seckadd(form.value).then((res: any) => {
                    if (res.code == 200) {
                        get()
                        close()
                        ElMessage({
                            message: res.msg,
                            type: 'success',
                        })
                    } else {
                        ElMessage({
                            message: res.msg,
                            type: 'warning',
                        })
                    }
                })

            } else {
                seckedit(form.value).then((res: any) => {
                    if (res.code == 200) {
                        get()
                        close()
                        ElMessage({
                            message: res.msg,
                            type: 'success',
                        })
                    } else {
                        ElMessage({
                            message: res.msg,
                            type: 'warning',
                        })
                    }
                })

            }
        }

    })


}


let form = ref({
    title: "",//限时秒杀名称
    begintime: "",//开始时间
    endtime: "",//结束时间
    first_cateid: "",//商品一级分类编号
    second_cateid: "",//商品二级分类编号
    goodsid: "",//商品编号
    status: 1,// 状态   1正常2禁用
    id: 0,

})
let time = ref()
let open = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    form.value = {
        title: "",//限时秒杀名称
        begintime: "",//开始时间
        endtime: "",//结束时间
        first_cateid: "",//商品一级分类编号
        second_cateid: "",//商品二级分类编号
        goodsid: "",//商品编号
        status: 1,// 状态   1正常2禁用
        id: 0

    }
    formEl.resetFields();
}
let date = (date: any) => {
    time.value = date
    form.value.begintime = time.value[0].getTime()
    form.value.endtime = time.value[1].getTime()
    // console.log(this.form);
}
// 关闭
let close = () => {
    emit("close")
}

const closeX = (done: any) => {
    done()
    close()
}
let change1 = (n: Boolean) => {
    let index = store.getters['cate/getCate'].findIndex((item: any) => {
        return item.id == form.value.first_cateid
    })
    cateArr.value = store.getters['cate/getCate'][index].children
    if (n) {
        form.value.second_cateid = ""
    }
}

let change2 = (n: Boolean) => {
    goodslist(null).then((res: any) => {
        shopList.value = res.list.filter((item: any) => {
            return item.second_cateid == form.value.second_cateid
        })
    })
    if (n) {
        form.value.goodsid = ""
    }


}
let rules = reactive<FormRules>({
    title: [
        { required: true, message: '请输入活动名称', trigger: 'blur' },
    ]
})
let getList = () => {
    specslist(null).then((res: any) => {
        specsList.value = res.list
    })
}
let lookup = (id: number) => {
    seckinfo({ id }).then((res: any) => {
        form.value = res.list
       form.value.id = id
        time.value = [new Date(Number(res.list.begintime)), new Date(Number(res.list.endtime))]
        change1(false)
        change2(false)
    })


}

getList()

let get = () => {
    store.dispatch("cate/getCateListAction")
    store.dispatch("specs/getSpecsListAction")
    store.dispatch("goods/getCountAction")
    store.dispatch("goods/getGoodsInfoActions")
    store.dispatch("seck/getSeckListActions")

}
get()
defineExpose({
    lookup
})


</script>